#include "../include/naryTree.h"
#include "../include/binsrchtree.h"
#include "../include/general_vfs_op.h"
#include "../include/header.h"
#include "../include/constants.h"
#include "../include/value_added.h"
#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int tree_dir(char *P1,char *P2)
{
int ret=-1;
    if(if_valid_args_(P1))
       if(if_valid_args_(P1));
         else return 0;
    else return 0;

    if(mainstr==NULL || mainstr->is_mounted!=5)
        return 3;
    remove_last_slash_(P1);
    ret=dir_tree_traversal(datastr->ntree,P1,P2);
    if(ret==7 || ret == 17)
        return 1;
    return ret;
}


int rename_file_(char *P1,char *P2)
{
   int ret=-1;
   int len1,len2;
   fdesc* temp_src_fd,*name_fd;
   char *path_temp;

   path_temp=(char*)malloc(sizeof(char)*CHARSIZE);   

    if(if_valid_args_(P1))
       if(if_valid_args_(P1));
         else return 0;
    else return 0;

   if(mainstr==NULL || mainstr->is_mounted!=5)
        return 3;

   if(!validate_name(P2))
     return 1;
    
   temp_src_fd=bst_search_fd(datastr->bst,P1);
   
 
   if(temp_src_fd==NULL)
     return 2;
   if(strcmp(temp_src_fd->f_type,"file")!=0)
         return 5;
 
   len1=strlen(strrchr(P1,'/'));
   len2=strlen(P1);
   strncpy(path_temp,P1,len2-len1);
   strcat(path_temp,"/");
   strcat(path_temp,P2); 
  
   name_fd=bst_search_fd(datastr->bst,path_temp);
   if(name_fd!=NULL)
     return 4;  

   else{
     ret=bst_remove(datastr->bst,temp_src_fd);
     //printf("ret is %d",ret);
     ret=delete_hashtable(datastr->htable,temp_src_fd);
   //printf("ret is %d",ret);
     strcpy(temp_src_fd->f_name,P2);
    //printf("ret is %d",ret);
     ret=bst_insert(datastr->bst,temp_src_fd);
 //printf("ret is %d",ret);
     ret=insert_hashtable(datastr->htable,temp_src_fd);
 //printf("ret is %d",ret);
     return 10;
  }
}
